package io.geekidea.boot.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.test.dto.PayOrderDto;
import io.geekidea.boot.test.entity.PayOrder;
import io.geekidea.boot.test.mapper.PayOrderMapper;
import io.geekidea.boot.test.query.PayOrderQuery;
import io.geekidea.boot.test.service.PayOrderService;
import io.geekidea.boot.test.vo.PayOrderVo;
import io.geekidea.boot.test.query.AppPayOrderQuery;
import io.geekidea.boot.test.vo.AppPayOrderVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 订单 服务实现类
 *
 * @author geekidea
 * @since 2025-03-26
 */
@Slf4j
@Service
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements PayOrderService {

    @Autowired
    private PayOrderMapper payOrderMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addPayOrder(PayOrderDto dto) {
        PayOrder payOrder = new PayOrder();
        BeanUtils.copyProperties(dto, payOrder);
        return save(payOrder);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updatePayOrder(PayOrderDto dto) {
        Long id = dto.getId();
        PayOrder payOrder = getById(id);
        if (payOrder == null) {
            throw new BusinessException("订单不存在");
        }
        BeanUtils.copyProperties(dto, payOrder);
        return updateById(payOrder);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deletePayOrder(Long id) {
        return removeById(id);
    }

    @Override
    public PayOrderVo getPayOrderById(Long id) {
        return payOrderMapper.getPayOrderById(id);
    }

    @Override
    public Paging<PayOrderVo> getPayOrderPage(PayOrderQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<PayOrderVo> list = payOrderMapper.getPayOrderPage(query);
        Paging<PayOrderVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppPayOrderVo getAppPayOrderById(Long id) {
        return payOrderMapper.getAppPayOrderById(id);
    }

    @Override
    public Paging<AppPayOrderVo> getAppPayOrderPage(AppPayOrderQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppPayOrderVo> list = payOrderMapper.getAppPayOrderPage(query);
        Paging<AppPayOrderVo> paging = new Paging<>(list);
        return paging;
    }

}
